﻿Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Net.Mail
Public Class ReporteClientes2
    Inherits System.Web.UI.Page

    Dim message As New MailMessage
    Dim smtp As New SmtpClient
    Dim i As Integer = 0


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnShow.Click
        CrystalReportViewer1.ReportSource = getReportDocument()
        CrystalReportViewer1.DataBind()
    End Sub

    ' Returns a ReportDocument object with the report and data loaded 
    Private Function getReportDocument() As ReportDocument

        ' File Path for Crystal Report
        Dim repFilePath As String = Server.MapPath("CrystalReport2.rpt")

        ' Declare a new Crystal Report Document object
        ' and the report file into the report document
        Dim repDoc As New ReportDocument()
        repDoc.Load(repFilePath)

        ' Set the datasource by getting the dataset from business layer
        ' In our case business layer is getCustomerData function
        repDoc.SetDataSource(getCustomerData())

        Return repDoc

    End Function

    Private Function getCustomerData() As DataSet
        Dim customerFilePath As String = Server.MapPath("Customers.xml")
        Dim ds As New DataSet()
        ds.ReadXml(customerFilePath)
        Return ds
    End Function

    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExport.Click
        ' Get the report document
        Dim repDoc As ReportDocument = getReportDocument()
        ' Stop buffering the response
        Response.Buffer = False
        ' Clear the response content and headers
        Response.ClearContent()
        Response.ClearHeaders()
        Try
            ' Export the Report to Response stream in PDF format and file name Customers
            ' There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports
            repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "Customers")
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            ex = Nothing
        End Try
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim direccion As String
        message.From = New MailAddress(TextBox1.Text)

        For i = 0 To ListBox1.Items.Count - 1
            message.To.Add(ListBox1.Items(i).Text)

        Next

        message.Body = TextBox4.Text
        message.Subject = TextBox5.Text
        message.Priority = MailPriority.Normal

        If (FileUpload1.HasFile) Then



            ListBox1.Items.Add("<br /> Nombre del Archivo: " & FileUpload1.FileName)


            Dim att As New System.Net.Mail.Attachment("C:\enviarCorreo\" & FileUpload1.FileName)
            message.Attachments.Add(att)
            smtp.EnableSsl = True
            smtp.Port = "587"
            smtp.Host = "smtp.live.com"
            smtp.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox3.Text)
            smtp.Send(message)


            Label1.Text = "El correo se ha enviado, correctamente"
        End If


    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        ListBox1.Items.Add(TextBox2.Text)

    End Sub
End Class